package com.jinzhi.jzweb.controller;


import java.util.*;

import cn.hutool.core.date.DateTime;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.ExamRegistrationDO;
import com.jinzhi.jzweb.domain.TopicReportDO;
import com.jinzhi.jzweb.service.TopicReportService;
import com.jinzhi.sys.dao.UserDao;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.DetailsReportDO;
import com.jinzhi.jzweb.service.DetailsReportService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2023-04-07 10:12:19 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/detailsReport")
public class DetailsReportController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private TopicReportService topicReportService;
	@Autowired
	private DetailsReportService detailsReportService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:detailsReport:detailsReport")
	String DetailsReport(){
	    return "jzweb/detailsReport/detailsReport";
	}

	@Log("专题搜索下拉框")
	@ResponseBody
	@GetMapping("/selTopicReportList")
	@RequiresPermissions("jzweb:detailsReport:detailsReport")
	public Result<List<TopicReportDO>> selTopicReportList(@RequestParam("name") String name,Model model) {
		Wrapper<TopicReportDO> wrapper = new EntityWrapper<TopicReportDO>().orderBy("sort_order",true).orderBy("id", false);
		if(StringUtil.notEmpty(name)){
			wrapper.like("topic_name", name);
		}
		List<TopicReportDO> topicReportDOS = topicReportService.selectList(wrapper);
		model.addAttribute("topicReportDOS",topicReportDOS);
		return Result.ok(topicReportDOS);
	}

	@Log("专区数据展示")
	@ResponseBody
	@GetMapping("/detailsReportList")
	@RequiresPermissions("jzweb:detailsReport:detailsReport")
	public Result<Page<DetailsReportDO>> detailsReportList(@RequestParam("topicId") String topicId,@RequestParam("type") String type){
		// 查询列表数据
		Wrapper<DetailsReportDO> wrapper = new EntityWrapper<DetailsReportDO>().orderBy("sort_order",true).orderBy("id", false);
		if(StringUtil.notEmpty(topicId)){
			wrapper.eq("topic_id", Long.parseLong(topicId));
		}
		if (StringUtil.notEmpty(type)) {
			wrapper.eq("type", type);
		}
		Page<DetailsReportDO> page = detailsReportService.selectPage(getPage(DetailsReportDO.class),wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			DetailsReportDO obj = (DetailsReportDO) page.getRecords().get(i);
			UserDO userDO = userService.selectById(obj.getUserId());
			obj.setUserName(userDO.getUsername());
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	@Log("进入专题详情新增页面")
	@GetMapping("/add/{type}")
	@RequiresPermissions("jzweb:detailsReport:add")
	String add(@PathVariable("type")String type,Model model){
		model.addAttribute("type",type);
	    return "jzweb/detailsReport/add";
	}

	@Log("新增专题详情")
	@ResponseBody
	@PostMapping("/saveDetailsReport")
	@RequiresPermissions("jzweb:detailsReport:add")
	public Result<String> saveDetailsReport( DetailsReportDO detailsReport){
		Boolean b = true;
		if(detailsReport.getTopicId() != null){
			detailsReport.setCreatedDate(new DateTime());
			detailsReport.setUserId(getUser().getId());
			detailsReport.setUserName(getUser().getName());
			detailsReportService.insert(detailsReport);
		}else {
			b = false;
		}
		return b?Result.ok():Result.fail("请选择具体专题");
	}

	@Log("进入详情页面")
	@GetMapping("/detailInfo/{id}")
	@RequiresPermissions("jzweb:detailsReport:detailsReport")
	String detail(@PathVariable("id") String id,Model model){
		DetailsReportDO detailsReport = detailsReportService.selectById(Long.parseLong(id));
		model.addAttribute("detailsReport", detailsReport);
		return "jzweb/detailsReport/detailInfo";
	}

	@Log("进入编辑页面")
	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:detailsReport:edit")
	String edit(@PathVariable("id") String id,Model model){
		DetailsReportDO detailsReport = detailsReportService.selectById(Long.parseLong(id));
		model.addAttribute("detailsReport", detailsReport);
	    return "jzweb/detailsReport/edit";
	}
	
	@Log("修改并保存编辑数据")
	@ResponseBody
	@RequestMapping("/updateDetailsReport")
	@RequiresPermissions("jzweb:detailsReport:edit")
	public Result<String>  updateDetailsReport( DetailsReportDO detailsReport){
		detailsReport.setUpdatedDate(new DateTime());
		boolean update = detailsReportService.updateById(detailsReport);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除单条数据")
	@PostMapping( "/removeDetailsReport")
	@ResponseBody
	@RequiresPermissions("jzweb:detailsReport:remove")
	public Result<String>  removeDetailsReport( String id){
		detailsReportService.deleteById(Long.parseLong(id));
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:detailsReport:batchRemove")
	public Result<String>  batchRemoveDetailsReport(@RequestParam("ids[]") Integer[] ids){
		detailsReportService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
